package com.todod.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.entity.ResultData;
import com.todod.model.Department;
import com.todod.model.User;
import com.todod.service.IDepartmentService;
import com.todod.service.IUserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jpf
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	private IDepartmentService service;
	@Autowired
	private IUserService service1;

	/**
	 * 树状结构
	 * 
	 * @return
	 */
	@RequestMapping("/getdepart")
	public ResultData getdepart(@RequestAttribute Long _userId) {
		User byId = service1.getById(_userId);
		Department byId2 = service.getById(byId.getDepartid());
		List<Department> departments = service.getdepart(byId.getDepartid());
		HashMap<String, Object> hashMap = new HashMap<>();
		hashMap.put("Department", byId2);
		hashMap.put("list", departments);
		return ResultData.success("ok", hashMap);
	}

	/**
	 * 获取部门列表
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ResultData list() {
		List<Department> departments = service.geList();
		return ResultData.success("ok", departments);
	}

	/**
	 * 树状结构并且没有被禁用的
	 * 
	 * @return
	 */
	@RequestMapping("/getdepartlist")
	public ResultData getdepartlist(@RequestAttribute Long _userId) {
		User byId = service1.getById(_userId);
		List<Department> departments = service.getdepart3(byId.getDepartid());
		return ResultData.success("ok", departments);
	}

	/**
	 * 通过子类的id查找到父类的级联id
	 */
	@RequestMapping("/getid")
	public ResultData get(Long id) {
		ArrayList<Long> arrayList = new ArrayList<>();
		Department department = service.getById(id);
		arrayList.add(department.getId());
		while (department.getParentId() != 0) {
			arrayList.add(department.getParentId());
			department = service.getById(department.getParentId());
		}
		return ResultData.success("ok", arrayList);
	}

	/**
	 * 添加部门
	 * 
	 * @return
	 */
	@RequestMapping("/adddpart")
	public ResultData adddpart(@RequestBody Department department, @RequestAttribute Long _userId) {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_name", department.getName());
		Department one = service.getOne(queryWrapper);
		if (one != null) {
			return ResultData.error(ResultData.ESTIMATE_ERROR_CODE, "企业名称已存在");
		}
		service.adddpart(department, _userId);
		return ResultData.success();
	}

	/**
	 * 通过部门id获取部门
	 * 
	 * @return
	 */

	@RequestMapping("/getparentName")
	public ResultData getparentName(Long id) {
		Department department = service.getById(id);
		return ResultData.success("ok", department);
	}

	/**
	 * 修改部门
	 * 
	 * @return
	 */
	@RequestMapping("/editdpart")
	public ResultData editdpart(@RequestBody Department department, @RequestAttribute Long _userId) {
		service.editdepart(department, _userId);
		return ResultData.success();
	}

	/**
	 * 启用
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/start")
	public ResultData start(Long id) {
		Department department = service.getById(id);
		department.setState("1");
		service.updateById(department);
		return ResultData.success();
	}

	/**
	 * 获取部门下子部门数量
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getchirldnum")
	public ResultData getchirldnum(Long id) {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_id", id);
		queryWrapper.eq("sd_state", "1");
		int count = service.count(queryWrapper);
		return ResultData.success("ok", count);
	}

	/**
	 * 获取部门下用户数量
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getdeUsernum")
	public ResultData getdeUsernum(Long id) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("su_department_id", id);
		queryWrapper.eq("su_state", "1");
		int count = service1.count(queryWrapper);
		return ResultData.success("ok", count);
	}

	/**
	 * 禁用
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/end")
	public ResultData end(Long id) {
		Department department = service.getById(id);
		department.setState("2");
		service.updateById(department);
		return ResultData.success();
	}

	/**
	 * 树状结构
	 * 
	 * @return
	 */
	@RequestMapping("/getdepart2")
	public ResultData getdepart2(@RequestAttribute Long _userId) {
		User byId = service1.getById(_userId);
		Department department = service.getById(byId.getDepartid());

		return ResultData.success("ok", department.getName());
	}

}
